<?php
class ContentWidget extends HomeAction{

	//内容列表
	/***
	 *参数：$data['l']=非分页调用条数	,$data['t']:分类栏目ID
	 *		$data['p']=分页内容调用条数
	***/

	public function render($data){

		$article=M('Article');

		$page=isset($_GET['p'])?$_GET['p']:null;	//分页ID，用于各分页面面生成缓存
		$type=isset($data['t'])?$data['t']:null;		
		$p=isset($data['p'])?$data['p']:null;	
		$cachename=md5('contentlist'.$type.'_'.$page.'_'.$p);
		$title=isset($data['title'])?$data['title']:14;
		$info=isset($data['info'])?$data['info']:225;


		//公用查询条件
		$sql['order']='id desc';
		if(isset($data['l'])){
			$sql['limit']=$data['l'];
		}

		if(S($cachename)){
				$list=S($cachename);
				if($list!==null){
					if($list['page']){
						$this->assign('page',$list['page']);
						unset($list['page']);
					}
				}
		}else{
			
				if($type){
					if(is_numeric($data['t'])){
						$sql['where']='typeid='.$type;
					}else{
						$sql['where']=array('in',$type);
					}	
				}

				if(!empty($p)){
					$list=$this->_select('article',$sql,$p,'list/p');
					if($list['page']){
						$page=$list['page'];unset($list['page']);
						$this->assign('page',$page);	
					}
				}else{
					$list=$this->_select('article',$sql);
				}

				
				if(!empty($list)){
					$list=$this->_url($list);
					$j=count($list);
					for($i=0;$i<$j;$i++){
						import('ORG.Util.String');
						$list[$i]['title']=String::msubstr($list[$i]['title'],0,$title,"utf-8",$suffix=false);
						$list[$i]['content']=String::msubstr(stripcslashes($list[$i]['content']),0,$info);

					}
					//获得栏目名
					foreach($list as $key => $val){
						$list[$key]['typename']=PublicAction::typename($val['typeid']);
					}
					//获得栏目URL
					foreach($list as $key => $val){
						$list[$key]['typeurl']=PublicAction::getUrl($val['typeid']);
					}
				}


				if($page){
					$list['page']=$page;
				}

				S($cachename,$list,F('cache'));	
		}	


		$this->assign('list',$list);
		if(isset($data['tpl'])){

			$this->display(dirname(__FILE__).'/'.$data['tpl'].'.html');

		}else{
			return $list;
		}
		


	}




}